//
//  SEGManageEmployeeViewModel.m
//  SEGViewModel
//
//  Created by 山刘 on 2020/11/25.
//

#import "SEGManageEmployeeViewModel.h"

@interface SEGManageEmployeeViewModel()
@property (nonatomic, strong, readwrite) RACCommand *requestCommand;
@property (strong, nonatomic, readwrite) RACSubject *messageSubject;

@property (nonatomic, strong, readwrite) RACCommand *auditCommand;
@end

@implementation SEGManageEmployeeViewModel
#pragma mark - Init
-(instancetype)init{
    if (self = [super init]) {
        [self racInit];
    }
    return self;
}

-(void)dealloc{
    [self.requestCommand rac_deallocDisposable];
    [self.messageSubject rac_deallocDisposable];
    
    [self.auditCommand rac_deallocDisposable];
}

-(NSArray*) logicWithPageNo:(NSInteger)pageNo sourceData:(NSArray*)sourceData {
    // 分页逻辑判断
    NSMutableArray<SEGSelectbycommonListModel *> *arrM = [NSMutableArray array];
    
    if (pageNo != 1) arrM = [NSMutableArray arrayWithArray:sourceData];
    
    [arrM addObjectsFromArray:[SEGSelectbycommonManager sharedManager].SelectByCommon.data.list];
    
    sourceData = [NSArray arrayWithArray:arrM];
    
    if (sourceData.count > 0) {
        [self hideCustomProgress];
    }else{
        [self showCustomProgress:@"暂无员工信息"];
    }
    
    return [NSArray arrayWithArray:arrM];
}

- (void)racInit {

    @weakify(self);
    _requestCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(NSMutableDictionary*  _Nullable input) {
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            @strongify(self);
            
            // Code here...
            [self isShowProgressHUD:YES];
            
            NSString * pageNo = [input valueForKey:@"pageNo"];
            NSString * status = [input valueForKey:@"status"];
            SEGQueryauthinfoRoleauthmapModel *data = [SEGQueryauthinfoManager sharedManager].QueryAuthInfo.data.userInfo;
            [[SEGSelectbycommonManager sharedManager] requestSelectbycommonWithCommunityid:kAccountCommunityID
                                                                              enterpriseId:data.enterpriseId
                                                                                    propId:data.propId
                                                                                  custName: [input valueForKey:@"custName"]
                                                                                    status:status
                                                                                   userTel:kAccountMobile
                                                                                    pageNo:pageNo
                                                                                  finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
                
                [self isShowProgressHUD:NO];
                
                if (!isSuccessed) {
                    
                    [self showCustomProgress:@"暂无员工信息"];
                    [self showMessage:errorMessage];
                    [subscriber sendError:nil];
                    return;
                    
                }
                
                if ([status isEqualToString:@"4"]) {
                    // 待审核
                    self.listVerify = [self logicWithPageNo:[pageNo integerValue] sourceData:self.listVerify];
                }else {
                    // 已认证
                    self.listNoAuth = [self logicWithPageNo:[pageNo integerValue] sourceData:self.listNoAuth];
                }
                
                [subscriber sendNext:status];
                [subscriber sendCompleted];
                return;
            }];

            return [RACDisposable disposableWithBlock:^{
            }];
        }];
    }];
    
    _auditCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(NSMutableDictionary*  _Nullable input) {
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            @strongify(self);
            
            // Code here...

            [self isShowProgressHUD:YES];
            
            [[SEGUserauditManager sharedManager] requestUserauditWithUsercustrelids:[input valueForKey:@"ids"]
                                                                            operate:[input valueForKey:@"operate"]
                                                                             roleId:[input valueForKey:@"roleId"]
                                                                           finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
                [self isShowProgressHUD:NO];
                
                if (!isSuccessed) {
                    
                    [self showMessage:errorMessage];
                    [subscriber sendNext:nil];
                    [subscriber sendCompleted];
                    return;
                }
                
                [subscriber sendNext:nil];
                [subscriber sendCompleted];
                return;
            }];

            return [RACDisposable disposableWithBlock:^{
            }];
        }];
    }];
}

#pragma mark - Publish Methods

#pragma mark - Private Methods
-(void)isShowProgressHUD:(BOOL)show {
    [self.messageSubject sendNext:@{@"code": show ? @"1" : @"2"}];
}
-(void)showMessage:(NSString*)msg {
    if (msg && msg.length > 0 ) [self.messageSubject sendNext:@{@"code":@"3",@"msg":msg}];
}
-(void)showCustomProgress:(NSString*)msg {
    [self.messageSubject sendNext:@{@"code":@"4",@"msg": (msg && msg.length > 0 ) ? msg : @"" }];
}

-(void)hideCustomProgress {
    [self.messageSubject sendNext:@{@"code":@"5",@"msg": @"" }];
}

#pragma mark - Getter
-(RACSubject *)messageSubject{
    if (!_messageSubject) {
        _messageSubject = [RACSubject subject];
    }
    return _messageSubject;
}

-(NSArray<SEGSelectbycommonListModel *> *)listVerify {
    if (!_listVerify) {
        _listVerify = [NSArray array];
    }
    return _listVerify;
}

-(NSArray<SEGSelectbycommonListModel *> *)listNoAuth {
    if (!_listNoAuth) {
        _listNoAuth = [NSArray array];
    }
    return _listNoAuth;
}
#pragma mark - Stter
@end
